/*
 * Copyright (C) 2008-2011 University of Deusto
 * 
 * All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.
 * 
 * This software consists of contributions made by many individuals, 
 * listed below:
 *
 * Author: Aitor Gómez Goiri <aitor.gomez@deusto.es>
 */
package otso.se.network.sockets.communication.abstraction;

import java.io.Serializable;
import java.util.Arrays;

public class HttpRequest implements Serializable {
	public static enum Operation {GET,POST,PUT,DELETE};
	
	final private Operation operation;
	// e.g. /spaces/http%3A%2F%2Fhow.lonely%2Fis-the-night%2Fwithout%2Fthe-howl%2Fof-a%2Fwolf%2F/graphs
	final private String url;
	// e.g. long timestamp (in HTTP "Date: Tue, 05 Jul 2011 17:58:22 GMT")
	final private long date;
	// e.g. Accept: application/json, across/multipart, text/n-triples, text/n3, application/rdf+xml, application/x-turtle, application/json
	private String accept;
	
	// Transfer-Encoding: chunked -> not accepted

	// e.g. Content-Length: 0
	private int contentLength;
	// e.g. Content-Type: text/n-triples; charset=UTF-8
	private String contentType;
	
	private byte[] data;
	
	//Host: localhost:8182
	//User-Agent: Restlet-Framework/2.0.5
	//Content-Length: 0
	
	public HttpRequest(Operation operation, String url, long date) {
		this.operation = operation;
		this.url = url;
		this.date = date;
	}

	public String getAccept() {
		return accept;
	}

	public void setAccept(String accept) {
		this.accept = accept;
	}

	public int getContentLength() {
		return contentLength;
	}

	private void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public Operation getOperation() {
		return operation;
	}

	public String getUrl() {
		return url;
	}

	public long getDate() {
		return date;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data.getBytes();
		setContentLength(this.data.length);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((accept == null) ? 0 : accept.hashCode());
		result = prime * result + contentLength;
		result = prime * result
				+ ((contentType == null) ? 0 : contentType.hashCode());
		result = prime * result + Arrays.hashCode(data);
		result = prime * result + (int) (date ^ (date >>> 32));
		result = prime * result
				+ ((operation == null) ? 0 : operation.hashCode());
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		HttpRequest other = (HttpRequest) obj;
		if (accept == null) {
			if (other.accept != null)
				return false;
		} else if (!accept.equals(other.accept))
			return false;
		if (contentLength != other.contentLength)
			return false;
		if (contentType == null) {
			if (other.contentType != null)
				return false;
		} else if (!contentType.equals(other.contentType))
			return false;
		if (!Arrays.equals(data, other.data))
			return false;
		if (date != other.date)
			return false;
		if (operation != other.operation)
			return false;
		if (url == null) {
			if (other.url != null)
				return false;
		} else if (!url.equals(other.url))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "HttpRequest [operation=" + operation.name() + ", url=" + url
				+ ", data=" + ((data!=null)?new String(data):"") + "]";
	}
}